//表示是组织查找或普通用户查找
var flag = location.pathname.split("\/")[3];

var param={
	pageIndex:1,
	pageSize:6,
	sortKey:'createDate',
	desc:'desc'
};
var userId=$("#userId").val();
var organization="";
var applyClaim="";
var activeStartTime="";
var sortKey="";
var activityType="";
var activityStatus="";
var keyWord="";
//标识是否通过组织用户ID查找
if (userId!=""||userId!=null){
    param.userId=userId;
}else {
    delete param["userId"];
}
$(function () {
	mui('.mui-scroll-wrapper').scroll({
		indicators: true //是否显示滚动条
	});
	//弹框的显示与隐藏
	mui('#ztModal').on('tap', '.cancel', function() {
		mui("#ztModal").popover('toggle');
	});
	mui('#typeModal').on('tap', '.cancel', function() {
		mui("#typeModal").popover('toggle');
	});
	mui('#sortModal').on('tap', '.cancel', function() {
		mui("#sortModal").popover('toggle');
	});
	mui('#sxModal').on('tap', '.cancel', function() {
		mui("#sxModal").popover('toggle');
	});


	mui('#sxModal').on('tap', '.confirm', function() {
		
		mui("#sxModal").popover('toggle');
		var org = $("[name=type1]:checked").attr("data");
		if(org==undefined)org="";
		param.organization=org;

		var applyscope=$("[name=type2]:checked").attr("data");
		if(applyscope==undefined)applyscope="";
		param.applyClaim=applyscope;
		var activeStartTime=$("[name=type3]:checked").attr("data");
		if(activeStartTime==undefined)activeStartTime="";
		param.activeStartTime=activeStartTime;
		param.pageIndex=1;
		loadActivitys();
	});


	mui('#sortModal').on('tap', '.confirm', function() {
		
		mui("#sortModal").popover('toggle');
		var type = $("[name=type5]:checked").attr("data");
		
		var checkedName = $("[name=type5]:checked").next("span").html();
		var area_idd = $("[name=type5]:checked").next().next().val();
		if(checkedName.length > 4) checkedName = checkedName.substr(0,4) + "..";
		$("#rulesName").html(checkedName);
		$("#area5").val(area_idd);
		if(type==undefined)type="";
		param.sortKey=type;
		param.pageIndex=1;
		//loadActivitys();
		searchOrder();
	});


	mui('#typeModal').on('tap', '.confirm', function() {
		
		mui("#typeModal").popover('toggle');
		var type = $("[name=type6]:checked").attr("data");
		var checkedName = $("[name=type6]:checked").next("span").html();
		var act_value= $("[name=type6]:checked").next().next().val();
		    	// alert(typeName);
		    	// alert(act_value);
		    	
		    	
		console.log(type);
		console.log(checkedName);
		console.log(act_value);
		$("#typeName").html(checkedName);
		$("#area6").val(act_value);
		if(type==undefined)type="";
		param.activityType=type;
		param.pageIndex=1;
		//loadActivitys();
		searchOrder();
	});

	mui('#ztModal').on('tap', '.confirm', function() {
		mui("#ztModal").popover('toggle');
		var type = $("[name=type4]:checked").attr("data");
		var checkedName = $("[name=type4]:checked").next("span").html();
		var audit_id= $("[name=type4]:checked").next().next().val();
		console.log(checkedName);
		$("#stateName").html(checkedName);
		$("#area4").val(audit_id);
		if(type==undefined)type="";
		param.activityStatus=type;
		param.pageIndex=1;
		//loadActivitys();
		searchOrder();
	});
	
	// $('#keyswordbut').on("tap",function(){
	// 	param.pageIndex=1;
	// 	var keysword=$("#keywords").val();
	// 	if(keysword==undefined)keysword="";
	// 	param.keyWord=keysword;
	// 	$("#ulNode").html("");
	// 	loadActivitys();
	// });
	$(document).keydown(function(e){
		if(e.keyCode === 13){
			param.pageIndex=1;
			var keysword=$("#keywords").val();
			if(keysword==undefined)keysword="";
			param.keyWord=keysword;
			$("#ulNode").html("");
			$("form").submit(function(){return false;});
			loadActivitys();
		}
	});
	
	//默认选择活动类型
	var levelcode = $("#levelcode").val();
	if(levelcode != ""){
		$('input[name="type6"]').each(function(){
		    var dataValue= $(this).attr("data");
		    if(dataValue == levelcode){
		    	$(this).attr("checked",true);
		    	var typeName= $(this).next("span").html();
		    	var act_value= $(this).next("input").val();
		    	alert(typeName);
		    	alert(act_value);
		    	$("#typeName").html(typact_valueeName);
		    	$("#cat6").val(act_value);
		    	param.activityType=levelcode;
		    }
		    
		});
	}
	
	
	loadActivitys();

})

/*function changeactivitySelect(typeName,parmName) {
	eval("param."+parmName+"=$(\"[name='"+typeName+"']:checked\").attr('data')");
	console.log(param);
}*/


loadActivitys = function () {
	$("#more").html("数据加载中...");
	$.post(config.findActivityForActivityList, param,function (responseData) {
		var data = JSON.parse(responseData);
		if(data==null){
			return;
		}
		activitys = data.data.actLi;
		if(activitys.length==0){
			$("#more").html("点击加载更多").hide();
			$("#nodata").show();
		}else{
			$("#more").html("点击加载更多").show();
			$("#nodata").hide();
		}
		var total=data.data.total;
		if(organization!=param.organization
			||applyClaim!=param.applyClaim
			||activeStartTime!=param.activeStartTime
			||sortKey!=param.sortKey
			||activityType!=param.activityType
			||activityStatus!=param.activityStatus
			||keyWord!=param.keyWord
		){
			$("#ulNode").html("");
			organization=param.organization;
			applyClaim=param.applyClaim;
			activeStartTime=param.activeStartTime;
			sortKey=param.sortKey;
			activityType=param.activityType;
			activityStatus=param.activityStatus;
			keyWord=param.keyWord;

		}
		for ( var j=0;j< activitys.length;j++){
			var i=activitys[j];
			if(i.shelveFlag == 0 || i.viewActivityStatus > 5){//判断活动是否下架，已下架不显示
				$("#more").html("点击加载更多").hide();
				$("#nodata").show();
				continue ;
			}
			//http://localhost:8000/actplus/webChat/activity/toActivityDetail?activityId=54dc93965e624a47863871a5d1564c8b
			var listNode=$("#listNode").clone(false);
			listNode.removeAttr("id");
			//如果是组织用户

			if(flag=="userCenter"){
                listNode.find("[name='detailHref']").attr("href",BASE_URL+"/webChat/activity/toAdminActivityDetail?activityId="+i.activityId);
			}else {
                listNode.find("[name='detailHref']").attr("href",BASE_URL+"/webChat/activity/toActivityDetail?activityId="+i.activityId);
            }
			listNode.css({display:'block'});
			listNode.find("[name='img']").attr("src",i.coverPicture);
			listNode.find("[name='title']").html(i.activityName);
			listNode.find("[name='address']").html(i.activityDetailAddress);
			
			
			var button="";
			var viewActivityStatus="";
			var activityApplyScopeFlag="";
			if(i.activityApplyScopeFlag==0 || i.activityApplyScopeFlag==null || i.activityApplyScopeFlag==undefined){
				activityApplyScopeFlag="公开活动";
			}else if(i.activityApplyScopeFlag==1 || i.activityApplyScopeFlag==null || i.activityApplyScopeFlag==undefined){
				activityApplyScopeFlag="内部活动";
			}else if(i.activityApplyScopeFlag==2 || i.activityApplyScopeFlag==null || i.activityApplyScopeFlag==undefined){
				activityApplyScopeFlag="团员活动";
			}
			if(i.activityViewMembers==null || i.activityViewMembers==undefined){
				i.activityViewMembers=0;
			}
			button=button+'<i class="icons-all icon_eye"></i>('+i.activityViewMembers+')'
				+'<span class="line">|</span><span class="color-red">'+activityApplyScopeFlag+'</span>';

			if(i.viewActivityStatus==1){
				viewActivityStatus="报名开始时间：<span>"+i.applyStartTimeStr+"</span>";
				button=button+'<span class="s-btn s-btn-red mui-pull-right">预热中</span>';
			}if(i.viewActivityStatus==2){
				if(i.activityFeeDetailsRemainMembers==0){
					viewActivityStatus="报名结束时间：<span>"+i.applyEndTimeStr+"</span>";
					button=button+'<span class="s-btn s-btn-yellow2 mui-pull-right">名额已满</span>';
				}else{
					viewActivityStatus="报名结束时间：<span>"+i.applyEndTimeStr+"</span>";
					button=button+'<span class="s-btn s-btn-green mui-pull-right">报名中</span>';
				}
				
			}if(i.viewActivityStatus==3){
				viewActivityStatus="活动开始时间：<span>"+i.startTimeStr+"</span>";
				button=button+'<span class="s-btn s-btn-yellow2 mui-pull-right">报名结束</span>';
			}if(i.viewActivityStatus==4){
				viewActivityStatus="活动结束时间：<span>"+i.endTimeStr+"</span>";
				button=button+'<span class="s-btn s-btn-blue mui-pull-right">活动中</span>';
			}if(i.viewActivityStatus==5 ||i.viewActivityStatus==0 ){
				viewActivityStatus="活动结束时间：<span>"+i.endTimeStr+"</span>";
				button=button+'<span class="s-btn mui-pull-right">活动结束</span>';
			}
			
			
			if(i.startTimeStr != null){
				listNode.find("[name='timeBlock']").html(ReplaceAll(i.startEndTimeStr,'/','.').replace('至',' - ').replace('连续','(连续)').replace('间隔','(间隔)'));
				listNode.find("[name='applystarttime']").html(viewActivityStatus);
			}

			
			/*listNode.find("[name='viewActivityStatus']").html(viewActivityStatus);
			listNode.find("[name='activityViewMembers']").html(i.activityViewMembers);
			var activityApplyDisplayScope="";
			if(i.activityApplyDisplayScope==0 || i.activityApplyDisplayScope==null || i.activityApplyDisplayScope==undefined){
				activityApplyDisplayScope="公开";
			}else{
				activityApplyDisplayScope="定向";
			}*/
			//listNode.find("[name='activityApplyDisplayScope']").html(i.activityApplyDisplayScope);
			listNode.find("[name='buttondiv']").html(button);
			$("#ulNode").append(listNode);
			if(i.rownum>=(total-1)){
				$("#more").hide();
				//$("#nodata").show();
			}/*else{
				$("#more").show();
				$("#nodata").hide();
			}*/
		}
		param.pageIndex =param.pageIndex+1;


		console.log(responseData);//string
	})



}


function ReplaceAll(str, sptr, sptr1){
	if(str != null ){
		while (str.indexOf(sptr) >= 0){
			str = str.replace(sptr, sptr1);
		}
	}
	return str;
}

//时间转换
function formatDateTime(inputTime) {
    var date = new Date(inputTime);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h = date.getHours();
    h = h < 10 ? ('0' + h) : h;
    var minute = date.getMinutes();
    var second = date.getSeconds();
    minute = minute < 10 ? ('0' + minute) : minute;
    second = second < 10 ? ('0' + second) : second;
    return y + '-' + m + '-' + d+' '+h+':'+minute+':'+second;
};